package com.huo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huo.dao.CheckitemDao;
import com.huo.entity.PageResult;
import com.huo.entity.QueryPageBean;
import com.huo.pojo.CheckItem;
import com.huo.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckitemService.class)
@Transactional
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckitemDao checkitemDao;

    /**
     * @Description:    检查项新增
     * @Author:        wangqingsong
     * @param:           [checkItem]
     * @return:          void
     * @CreateDate:     2020/12/17 15:18
     */
    @Override
    public void add(CheckItem checkItem) {
        checkitemDao.add(checkItem);
    }

    /**
     * @Description:    检查项分页
     * @Author:        wangqingsong
     * @param:           [queryPageBean]
     * @return:          com.huo.entity.PageResult
     * @CreateDate:     2020/12/19 13:43
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        if (StrUtil.isNotBlank(queryPageBean.getQueryString())) {
            PageHelper.startPage(1, queryPageBean.getPageSize());
            Page<CheckItem> page = checkitemDao.selectByQueryString(queryPageBean.getQueryString());
            return new PageResult(page.getTotal(), page.getResult());
        }
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckItem> page = checkitemDao.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * @Description:    检查项删除
     * @Author:        wangqingsong
     * @param:           [id]
     * @return:          void
     * @CreateDate:     2020/12/19 19:04
     */
    @Override
    public void deleteById(Integer id) {
        long count = checkitemDao.findCountById(id);
        if (count > 0) {
            throw new RuntimeException("该检查项与检查组有关联，不可删除!");
        }
        checkitemDao.deleteById(id);
    }

    /**
     * @Description:    根据id查询检查项
     * @Author:        wangqingsong
     * @param:           [id]
     * @return:          com.huo.pojo.CheckItem
     * @CreateDate:     2020/12/20 15:22
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkitemDao.findById(id);
    }

    /**
     * @Description:    检查项编辑
     * @Author:        wangqingsong
     * @param:           [checkItem]
     * @return:          void
     * @CreateDate:     2020/12/21 18:13
     */
    @Override
    public void edit(CheckItem checkItem) {
        checkitemDao.edit(checkItem);
    }

    /**
     * @Description: java类作用描述
     * @Author: 汪青松
     * @Param: []
     * @Return: java.util.List<com.huo.pojo.CheckItem>
     * @CreateDate: 2021/1/15 17:25
     */
    @Override
    public List<CheckItem> findAll() {
        return checkitemDao.findAll();
    }
}
